/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.lczyfz.wisdom.modules.platform.service;

import com.lczyfz.wisdom.common.persistence.Page;
import com.lczyfz.wisdom.common.service.CrudService;
import com.lczyfz.wisdom.common.utils.ServerResponse;
import com.lczyfz.wisdom.common.utils.StringUtils;
import com.lczyfz.wisdom.modules.platform.dao.AddressDao;
import com.lczyfz.wisdom.modules.platform.entity.Address;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * addressService
 * @author zkh
 * @version 2019-09-26
 */
@Service
@Transactional(readOnly = true)
public class AddressService extends CrudService<AddressDao, Address> {

	@Autowired
	AddressDao addressDao;

	public Address get(String id) {
		return super.get(id);
	}
	
	public List<Address> findList(Address address) {
		return super.findList(address);
	}
	
	public Page<Address> findPage(Page<Address> page, Address address) {
		return super.findPage(page, address);
	}

	public ServerResponse<Address> getInfoByUserId(String userId) {
		ServerResponse<Address> serviceResult = new ServerResponse<Address>(0,"获取用户地址成功");
		try {
			List<Address> addressList = addressDao.findAddressByUser(userId);
			if (addressList.size() > 0){
				serviceResult.setDataList(addressList);
			}else{
				return  new ServerResponse<>(0,"当前用户的无收货地址信息",new ArrayList<>());
			}

		}catch (Exception e){
			e.printStackTrace();
			return  new ServerResponse<>(-10000,"获取当前用户收货地址信息失败",new ArrayList<>());
		}
		return serviceResult;
	}

	@Transactional(readOnly = false)
	public ServerResponse<Object> saveAddress(Address address) {
		ServerResponse<Object> serviceResult = new ServerResponse<Object>(0,"收货地址添加成功");
		int flag = 0;
		try {
			if(null == address.getUserName() || "".equals(address.getUserName())){
				return new ServerResponse(-10000,"用户姓名不能为空");
			}else if(null == address.getUMobile() || "".equals(address.getUMobile())){
				return new ServerResponse(-10001,"用户电话不能为空");
			}else if(null == address.getDetailAdd() || "".equals(address.getDetailAdd())){
				return new ServerResponse(-10002,"用户详细地址不能为空");
			}
			serviceResult.setMsgCode(0);
			if (StringUtils.isBlank(address.getId())){//新增
				List<Address> addressList = addressDao.findAddressByUser(address.getUserId());
				if (addressList.size()<1){
					address.setIsdefault("1");
				}else {
					address.setIsdefault("0");
				}
				serviceResult.setErrMsg("收获地址添加成功");
			}else {
				serviceResult.setErrMsg("收获地址修改成功");
				flag = 1;
			}
			super.save(address);
			serviceResult.setData(address);
		}catch (Exception e){
			e.printStackTrace();
			if (flag == 0){
				return  new ServerResponse<Object>(-10001,"收获地址添加失败");
			}else {
				return  new ServerResponse<Object>(-10001,"收获地址修改失败");
			}
		}

		return serviceResult;
	}
	
	@Transactional(readOnly = false)
	public ServerResponse<Object> deleteAddress(String id) {
		ServerResponse<Object> serviceResult = new ServerResponse<Object>(0,"收获地址删除成功");
		try {
			int result = addressDao.deleteAddress(id);
			if (result == 1){
				serviceResult.setMsgCode(0);
				serviceResult.setErrMsg("收获地址删除成功");
			}else{
				serviceResult.setMsgCode(-10001);
				serviceResult.setErrMsg("收获地址删除失败,请联系管理员");
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return serviceResult;
	}

	@Transactional(readOnly = false)
	public ServerResponse<Object> updateIsDefault(String id,String userId) {
		ServerResponse<Object> serviceResult = new ServerResponse<Object>(0,"修改默认地址成功");
		try {
			String defaultId = "";
			List<Address> addressList = addressDao.findAddressByUser(userId);
			if(addressList.size()>2){
				for (int i=0;i<addressList.size();i++){
					if ("1".equals(addressList.get(i).getIsdefault())){
						defaultId = addressList.get(i).getId();
					}
				}
			}
			int result1 = addressDao.updateIsDefault(defaultId,"0");//将前一个默认地址修改成非默认
			int result2 = addressDao.updateIsDefault(id,"1");
			if (result1 == result2){
				serviceResult.setMsgCode(0);
				serviceResult.setErrMsg("修改默认地址成功");
			}else{
				serviceResult.setMsgCode(-10001);
				serviceResult.setErrMsg("修改默认地址失败,请联系管理员");
			}
		}catch (Exception e){
			e.printStackTrace();
			return  new ServerResponse<>(-10002,"修改默认地址失败,请联系管理员");
		}
		return serviceResult;
	}



}